package cj.web.sc.dao;

import java.util.List;
import java.util.Map;

import net.paoding.rose.jade.annotation.DAO;
import net.paoding.rose.jade.annotation.ReturnGeneratedKeys;
import net.paoding.rose.jade.annotation.SQL;

@DAO
public interface UserDAO {

	@SQL("insert into Member(phone,deleteFlag,createUser,createAt,updateUser,updateAt) values(:1,1,:1,NOW(),:1,NOW())")
	void register(String phone);
	
	@SQL("select count(0) from Member where deleteFlag=1 and phone=:1")
	int isRegister(String phone);
	
	@SQL("select id from Member where deleteFlag=1 and phone=:1")
	int getBuyerId(String phone);
	
	@SQL("select phone from Member where id=:1")
	String getPhoneById(int id);

	@SQL("select id,receiverName,receiverPhone,province,city,area,address from SCAddress where buyerId=:1 and deleteFlag=1 and isDefault=1")
	List<Map> getDefaultAddress(int buyerId);

	@SQL("select id,receiverName,receiverPhone,isDefault,province,city,area,address from SCAddress where deleteFlag=1 and buyerId=:1 order by isDefault desc")
	List<Map> getAddrByBuyerId(int buyerId);

	@SQL("select id,buyerId,receiverName,receiverPhone,isDefault,province,city,area,address from SCAddress where id=:1")
	List<Map> getAddressById(String addressId);

	@ReturnGeneratedKeys
	@SQL("insert into SCAddress(buyerId,receiverName,receiverPhone,province,city,area,address,isDefault,deleteFlag,createAt,createUser,UpdateAt,UpdateUser) values(:1,:2,:3,:4,:5,:6,:7,:8,1,NOW(),:8,NOW(),:8)")
	int createAddress(int buyerId, String receiverName, String receiverPhone,
			String province, String city, String area, String address,
			int isDefault, String phone);

	@SQL("update SCAddress set receiverName=:2,receiverPhone=:3,province=:4,city=:5,area=:6,address=:7,isDefault=:8,updateAt=NOW() where id=:1")
	void updateAddress(String id, String receiverName, String receiverPhone,
			String province, String city, String area, String address,
			int isDefault);

	@SQL("update SCAddress set deleteFlag=0,updateAt=NOW() where id=:1")
	void delAddrById(int addressId);

	@SQL("update SCAddress set isDefault=0 where buyerId=:1 and isDefault=1 and deleteFlag=1")
	void notDefaultByBuyerId(int buyerId);

	@SQL("update SCAddress set isDefault=1 where id=:1")
	void setDefalut(String addressId);

	@SQL("select count(1) from SCAddress where deleteFlag=1 and buyerId=:1")
	int addrCntByBuyerId(int buyerId);

}
